// ConfigDlg.cpp : implementation of the CAboutDlg class
//
/////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "resource.h"

#include "configdlg.h"


#define DEFAULT_BPS		115200


// popular bps value
typedef struct {
	TCHAR *label;
	DWORD bps;
} BPS_TABLE;

static BPS_TABLE tbl[] = {
	{	TEXT("300bps"), 300 },
	{	TEXT("1200bps"), 1200 },
	{	TEXT("2400bps"), 2400 },
	{	TEXT("4800bps"), 4800 },
	{	TEXT("9600bps"), 9600 },
	{	TEXT("19.2Kbps"), 19200 },
	{	TEXT("38.4Kbps"), 38400 },
	{	TEXT("57.6Kbps"), 57600 },
	{	TEXT("115.2Kbps"), 115200 },
	{	TEXT("120Kbps"), 120000 },		// this is prepared for WILLCOM W-SIM
	{	TEXT("230.4Kbps"), 230400 },
	{   NULL, 0 }
};


LRESULT CConfigDlg::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	int index, defindex;

	CenterWindow(GetParent());

	// Setup Combo
	m_ComboBox.Attach(GetDlgItem(IDC_COMBO2));

	defindex = -1;
	m_numbps = 0;
	for (index = 0; tbl[index].bps != 0; index++) {
		m_ComboBox.InsertString(index, tbl[index].label);
		m_ComboBox.SetItemData(index, tbl[index].bps);
		m_numbps = index + 1;	// number of table item, updated in each time
		if (tbl[index].bps == DEFAULT_BPS)
			defindex = index;
	}

	// set default selection in Combo
	if (defindex >= 0)
		m_ComboBox.SetCurSel(defindex);
	else
		m_ComboBox.SetCurSel(0);	// the 1st one

	return TRUE;
}

LRESULT CConfigDlg::OnCloseCmd(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	int index;

	if (wID = IDOK) {
		// read Combo value and set
		index = m_ComboBox.GetCurSel();
		if (0<= index && index < m_numbps) {
			DefaultDCB.BaudRate = tbl[index].bps;
			DEBUGMSG(1, (TEXT("Default BPS set to %d bps\r\n"), DefaultDCB.BaudRate));
		}
	}

	EndDialog(wID);
	return 0;
}
